<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>网站性能优化（Yahoo军规） | Xiaoming&#39;s Blog | 一入前端深似海，从此妹子是路人。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="WPO,网站性能优化,Gzip 压缩,CDN">
    <meta name="description" content="压缩 HTML、CSS、JS代码压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也会在文档中添加多余的字节。 使用压缩工具可以非常简单地把无用的字节从你的 CSS、JS 和 HTML 文件修剪掉。一般使用工具进行压缩 Gzip、Brotli chrome控制台检查js代码，有个小小的“{}”符号，可以将压缩的代码格式化（for">
<meta name="keywords" content="WPO,网站性能优化,Gzip 压缩,CDN">
<meta property="og:type" content="article">
<meta property="og:title" content="网站性能优化（Yahoo军规）">
<meta property="og:url" content="http://www.lovebxm.com/2017/05/30/WPO/index.html">
<meta property="og:site_name" content="Xiaoming&#39;s Blog">
<meta property="og:description" content="压缩 HTML、CSS、JS代码压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也会在文档中添加多余的字节。 使用压缩工具可以非常简单地把无用的字节从你的 CSS、JS 和 HTML 文件修剪掉。一般使用工具进行压缩 Gzip、Brotli chrome控制台检查js代码，有个小小的“{}”符号，可以将压缩的代码格式化（for">
<meta property="og:image" content="https://gpic.qpic.cn/gbar_pic/mnFqzbVGyMo9D22OJekiaMnfxNCwqFjCvXH8Qtia3PrGicjujLlc0Olnw/1000">
<meta property="og:image" content="http://oph264zoo.bkt.clouddn.com/17-5-8/11541491-file_1494256942464_1131.png">
<meta property="og:image" content="http://oph264zoo.bkt.clouddn.com/17-5-8/86632069-file_1494256742859_1c55.png">
<meta property="og:image" content="http://oph264zoo.bkt.clouddn.com/17-5-8/68054722-file_1494256597378_8708.png">
<meta property="og:image" content="http://oph264zoo.bkt.clouddn.com/17-5-9/34009491-file_1494260030825_b480.png">
<meta property="og:updated_time" content="2017-05-30T09:47:44.240Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="网站性能优化（Yahoo军规）">
<meta name="twitter:description" content="压缩 HTML、CSS、JS代码压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也会在文档中添加多余的字节。 使用压缩工具可以非常简单地把无用的字节从你的 CSS、JS 和 HTML 文件修剪掉。一般使用工具进行压缩 Gzip、Brotli chrome控制台检查js代码，有个小小的“{}”符号，可以将压缩的代码格式化（for">
<meta name="twitter:image" content="https://gpic.qpic.cn/gbar_pic/mnFqzbVGyMo9D22OJekiaMnfxNCwqFjCvXH8Qtia3PrGicjujLlc0Olnw/1000">
    
        <link rel="alternate" type="application/atom+xml" title="Xiaoming&#39;s Blog" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu"  >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">白小明</h5>
          <a href="mailto:80583600@qq.com" title="80583600@qq.com" class="mail">80583600@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hand-peace-o"  >
                <i class="icon icon-lg icon-hand-peace-o"></i>
                作品
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/bxm0927" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/user"  >
                <i class="icon icon-lg icon-user"></i>
                关于我
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/404.html"  >
                <i class="icon icon-lg icon-link"></i>
                404 测试
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">网站性能优化（Yahoo军规）</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">网站性能优化（Yahoo军规）</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-05-30T09:35:20.819Z" itemprop="datePublished" class="page-time">
  2017-05-30
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#压缩-HTML、CSS、JS代码"><span class="post-toc-number">1.</span> <span class="post-toc-text">压缩 HTML、CSS、JS代码</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#正确放置-CSS-（页面上面）"><span class="post-toc-number">2.</span> <span class="post-toc-text">正确放置 CSS （页面上面）</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#正确放置-Javascript-（页面下面）"><span class="post-toc-number">3.</span> <span class="post-toc-text">正确放置 Javascript （页面下面）</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#启用-Gzip-压缩"><span class="post-toc-number">4.</span> <span class="post-toc-text">启用 Gzip 压缩</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使用预先获取"><span class="post-toc-number">5.</span> <span class="post-toc-text">使用预先获取</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使用-CDN"><span class="post-toc-number">6.</span> <span class="post-toc-text">使用 CDN</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#减少DOM操作次数"><span class="post-toc-number">7.</span> <span class="post-toc-text">减少DOM操作次数</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#JS-少用全局变量，缓存DOM节点查找的结果"><span class="post-toc-number">8.</span> <span class="post-toc-text">JS 少用全局变量，缓存DOM节点查找的结果</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#尽可能的减少-HTTP-请求数"><span class="post-toc-number">9.</span> <span class="post-toc-text">尽可能的减少 HTTP 请求数</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#避免使用CSS-Expressions-（CSS-表达式）"><span class="post-toc-number">10.</span> <span class="post-toc-text">避免使用CSS Expressions （CSS 表达式）</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#把js和css都写在外部文件中（酌情）"><span class="post-toc-number">11.</span> <span class="post-toc-text">把js和css都写在外部文件中（酌情）</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#减少-DNS-查询"><span class="post-toc-number">12.</span> <span class="post-toc-text">减少 DNS 查询</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#避免重定向"><span class="post-toc-number">13.</span> <span class="post-toc-text">避免重定向</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#移除重复的脚本"><span class="post-toc-number">14.</span> <span class="post-toc-text">移除重复的脚本</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#配置实体标签-ETags"><span class="post-toc-number">15.</span> <span class="post-toc-text">配置实体标签 ETags</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#使-AJAX-缓存"><span class="post-toc-number">16.</span> <span class="post-toc-text">使 AJAX 缓存</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#添加-Expire-Cache-Control-头"><span class="post-toc-number">17.</span> <span class="post-toc-text">添加 Expire/Cache-Control 头</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-WPO"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">网站性能优化（Yahoo军规）</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-05-30 17:35:20" datetime="2017-05-30T09:35:20.819Z"  itemprop="datePublished">2017-05-30</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="压缩-HTML、CSS、JS代码"><a href="#压缩-HTML、CSS、JS代码" class="headerlink" title="压缩 HTML、CSS、JS代码"></a>压缩 HTML、CSS、JS代码</h2><p>压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也会在文档中添加多余的字节。</p>
<p>使用<strong>压缩工具</strong>可以非常简单地把无用的字节从你的 CSS、JS 和 HTML 文件修剪掉。一般使用工具进行压缩 Gzip、Brotli</p>
<p>chrome控制台检查js代码，有个小小的“{}”符号，可以将<strong>压缩的代码格式化（format）</strong>，还挺好用的。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="https://gpic.qpic.cn/gbar_pic/mnFqzbVGyMo9D22OJekiaMnfxNCwqFjCvXH8Qtia3PrGicjujLlc0Olnw/1000" alt="image" title="">
                </div>
                <div class="image-caption">image</div>
            </figure>
<a id="more"></a>
<h2 id="正确放置-CSS-（页面上面）"><a href="#正确放置-CSS-（页面上面）" class="headerlink" title="正确放置 CSS （页面上面）"></a>正确放置 CSS （页面上面）</h2><p>避免页面空白或闪烁。</p>
<p>把 CSS 放在 HTML 的上面部分，文档头之内，这可以确保正常的渲染过程。</p>
<p>这个策略不能提高网站的加载速度，但它不会让访问者长时间看着空白屏幕或者无格式的文本（FOUT）等待。如果网页大部分可见元素已经加载出来了，访问者才更有可能等待加载整个页面，从而带来对前端的优化效果。这就是<strong>知觉性能</strong>。</p>
<p>此外，不少网站管理员在网页中错误的使用 @import指令 来引入外部样式表。这是一个过时的方法，它会阻止浏览并行下载。link 标签才是最好的选择，它也能提高网站的前端性能。多说一句，通过 link 标签请求加载的外部样式表不会阻止并行下载。</p>
<h2 id="正确放置-Javascript-（页面下面）"><a href="#正确放置-Javascript-（页面下面）" class="headerlink" title="正确放置 Javascript （页面下面）"></a>正确放置 Javascript （页面下面）</h2><p>如果将 JavaScript 放置在 head 标签内或 HTML 文档的上部，这会阻塞 HTML 和 CSS 元素的加载过程。这个错误会导致页面加载时间增长，增加用户等待时间，容易让人感到不耐烦而放弃对网站的访问。</p>
<p>可以将JavaScript属性<strong>置于HTML底部</strong>来避免此问题。</p>
<h2 id="启用-Gzip-压缩"><a href="#启用-Gzip-压缩" class="headerlink" title="启用 Gzip 压缩"></a>启用 Gzip 压缩</h2><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oph264zoo.bkt.clouddn.com/17-5-8/11541491-file_1494256942464_1131.png" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>
<h2 id="使用预先获取"><a href="#使用预先获取" class="headerlink" title="使用预先获取"></a>使用预先获取</h2><p><a href="http://www.cnblogs.com/suyuwen1/p/5506397.html" target="_blank" rel="external">link</a></p>
<p>预先获取可以在真正需要之前通过取得必需的资源和相关数据来改善访问用户的浏览体验，主要有3类预先获取:<strong>链接预先获取</strong>、<strong>DNS 预先获取</strong>、<strong>预先渲染</strong>,在 HTML 中的链接属性上增加 rel=”prefetch”、rel=”dns-prefetch”或者 rel=”prerender” 标记。<br><strong>链接预先获取</strong>：在HTML5中,有个很有用但常被忽略的特性，就是预先加载(prefetch)，它的原理是：利用浏览器的空闲时间去先下载用户指定需要的内容，然后缓存起来，这样用户下次加载时，就直接从缓存中取出来，效率就快了。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"prerender"</span> <span class="attr">href</span>=<span class="string">"http://www.example.com/"</span>&gt;</span> <span class="tag">&lt;<span class="name">!-Chrome</span> <span class="attr">--</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"prefetch"</span> <span class="attr">href</span>=<span class="string">"http://www.example.com/"</span>&gt;</span>  <span class="tag">&lt;<span class="name">!-Firefox</span> <span class="attr">--</span>&gt;</span></div></pre></td></tr></table></figure>
<p>即使在不支持的浏览器,用了这个特性其实是不会出错的,只不过浏览器解析不到而已。<br>如果你感觉能有办法预先预测到用户期望点的页面(比如用户看最新的受欢迎的热图,他 可能看了第一页后,会继续看下一页)，这个时候就可以用预先加载这个特性了。比如</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"prefetch"</span> <span class="attr">href</span>=<span class="string">"/images/test.jpg"</span>/&gt;</span> <span class="tag">&lt;<span class="name">!-取图片</span> <span class="attr">--</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"prefetch"</span> <span class="attr">href</span>=<span class="string">"&lt;?php echo get_next_posts_page_link(); ?&gt;"</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="使用-CDN"><a href="#使用-CDN" class="headerlink" title="使用 CDN"></a>使用 CDN</h2><p>CDN的全称是Content Delivery Network，即内容分发网络，内容分发网络能显著提高网站的速度和性能。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oph264zoo.bkt.clouddn.com/17-5-8/86632069-file_1494256742859_1c55.png" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>
<h2 id="减少DOM操作次数"><a href="#减少DOM操作次数" class="headerlink" title="减少DOM操作次数"></a>减少DOM操作次数</h2><h2 id="JS-少用全局变量，缓存DOM节点查找的结果"><a href="#JS-少用全局变量，缓存DOM节点查找的结果" class="headerlink" title="JS 少用全局变量，缓存DOM节点查找的结果"></a>JS 少用全局变量，缓存DOM节点查找的结果</h2><h2 id="尽可能的减少-HTTP-请求数"><a href="#尽可能的减少-HTTP-请求数" class="headerlink" title="尽可能的减少 HTTP 请求数"></a>尽可能的减少 HTTP 请求数</h2><figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oph264zoo.bkt.clouddn.com/17-5-8/68054722-file_1494256597378_8708.png" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>
<h2 id="避免使用CSS-Expressions-（CSS-表达式）"><a href="#避免使用CSS-Expressions-（CSS-表达式）" class="headerlink" title="避免使用CSS Expressions （CSS 表达式）"></a>避免使用CSS Expressions （CSS 表达式）</h2><p>CSS表达式（CSS Expression），又称动态属性（Dynamic properties）。</p>
<p>在页面显示或缩放、鼠标移动、页面滚动的时候，css表达式都会重新计算一次，严重影响网页性能。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span><span class="undefined"></span></div><div class="line">    ul a &#123;</div><div class="line">        /* IE 6 7 8 */</div><div class="line">      width: expression(this.offsetWidth &gt; 750? add() : add());</div><div class="line">    &#125;</div><div class="line">  <span class="tag">&lt;/<span class="name">style</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">ul</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">""</span>&gt;</span>Lorem ipsum dolor sit amet.<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">""</span>&gt;</span>Lorem ipsum dolor sit amet.<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">""</span>&gt;</span>Lorem ipsum dolor sit amet.<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">p</span>&gt;</span>当鼠标移动时，CSS 表达式计算了<span class="tag">&lt;<span class="name">b</span> <span class="attr">id</span>=<span class="string">"run"</span>&gt;</span><span class="tag">&lt;/<span class="name">b</span>&gt;</span>次<span class="tag">&lt;/<span class="name">p</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span></div><div class="line">    <span class="keyword">var</span> i = <span class="number">0</span>;</div><div class="line">    <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>) </span>&#123;</div><div class="line">      i++;</div><div class="line">      <span class="built_in">document</span>.getElementById(<span class="string">'run'</span>).innerHTML = i;</div><div class="line">      <span class="keyword">return</span>;</div><div class="line">    &#125;</div><div class="line">  <span class="tag">&lt;/<span class="name">script</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="把js和css都写在外部文件中（酌情）"><a href="#把js和css都写在外部文件中（酌情）" class="headerlink" title="把js和css都写在外部文件中（酌情）"></a>把js和css都写在外部文件中（酌情）</h2><p>CSS、JS写在页面内的情况：</p>
<ul>
<li>只应用于一个页面。</li>
<li>不经常被访问到。</li>
<li>脚本和样式很少（20行以下时）。</li>
</ul>
<h2 id="减少-DNS-查询"><a href="#减少-DNS-查询" class="headerlink" title="减少 DNS 查询"></a>减少 DNS 查询</h2><p>DNS（Domain Name System，域名系统）</p>
<p>通过DNS查找到域名对应的ip地址，一次大约60ms</p>
<h2 id="避免重定向"><a href="#避免重定向" class="headerlink" title="避免重定向"></a>避免重定向</h2><p>无论是通过服务器端重定向还是通过JavaScript代码进行内容重定向，网站都会首先加载一个空白的页面，然后再定向到另外的页面，这样不仅延长了页面的加载时间，还很有可能导致无法跳转，让用户面对一个“空白”页面。</p>
<p>定义：原始请求被重新转到了其他请求</p>
<p>301：被移动到另外位置（永久重定向）使搜索引擎智能,不需要从旧地址到新地址，删除旧地址，直接到新地址</p>
<p>302：页面被找到，但不在原始位置（临时重定向）先到旧地址，再到新地址</p>
<p>重定向：重新请求下载资源，增加了http请求</p>
<h2 id="移除重复的脚本"><a href="#移除重复的脚本" class="headerlink" title="移除重复的脚本"></a>移除重复的脚本</h2><p>重复引用的脚本，会执行多次</p>
<h2 id="配置实体标签-ETags"><a href="#配置实体标签-ETags" class="headerlink" title="配置实体标签 ETags"></a>配置实体标签 ETags</h2><p>使用一种标志表示一种资源是否做了修改，可以减少服务器的响应。</p>
<p>详细点就是：浏览器想服务器要某种资源，服务器一看，判断出了这种资源已经给过浏览器了，并且在服务器端的也没有变化，所以浏览器可以使用他自身就有的，然后告诉浏览器，浏览器回到自身找找，找到啦就用了，这样浏览器和服务器之间会有对话不过不用服务器再重复的给浏览器资源了！</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://oph264zoo.bkt.clouddn.com/17-5-9/34009491-file_1494260030825_b480.png" alt="" title="">
                </div>
                <div class="image-caption"></div>
            </figure>
<h2 id="使-AJAX-缓存"><a href="#使-AJAX-缓存" class="headerlink" title="使 AJAX 缓存"></a>使 AJAX 缓存</h2><p>AJAX 可使页面局部异步更新。</p>
<p>get 同一地址不重复执行，可以被缓存</p>
<p>post 不会被缓存，每次都会传送到服务器那里，一般返回200；</p>
<h2 id="添加-Expire-Cache-Control-头"><a href="#添加-Expire-Cache-Control-头" class="headerlink" title="添加 Expire/Cache-Control 头"></a>添加 Expire/Cache-Control 头</h2>
        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-05-30T09:47:44.240Z" itemprop="dateUpdated">2017-05-30 17:47:44</time>
</span><br>


        
        原始文章链接：<a href="/2017/05/30/WPO/" target="_blank" rel="external">http://www.lovebxm.com/2017/05/30/WPO/</a>
        
    </div>
    <footer>
        <a href="http://www.lovebxm.com">
            <img src="/img/avatar.jpg" alt="白小明">
            白小明
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/CDN/">CDN</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Gzip-压缩/">Gzip 压缩</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/WPO/">WPO</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/网站性能优化/">网站性能优化</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://www.lovebxm.com/2017/05/30/WPO/&title=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&pic=http://www.lovebxm.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://www.lovebxm.com/2017/05/30/WPO/&title=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&source=压缩 HTML、CSS、JS代码压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://www.lovebxm.com/2017/05/30/WPO/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&url=http://www.lovebxm.com/2017/05/30/WPO/&via=http://www.lovebxm.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://www.lovebxm.com/2017/05/30/WPO/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/05/30/mysqlCommon/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">MySQL 安装（Windows）及常用命令</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/05/30/buildBlog/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">可能是最详细的 Hexo + GitHub Pages 搭建博客的教程</h4>
      </a>
    </div>
  
</nav>



    










    <div id="cloud-tie-wrapper" class="comments cloud-tie-wrapper"></div>
    <script>
    var cloudTieConfig = {
        url: 'http://www.lovebxm.com/2017/05/30/WPO/index.html',
        sourceId: 'WPO',
        productKey: '294c776acae94037bdd6605780e549e4',
        target: 'cloud-tie-wrapper',
        pcFiles: [
            '//unpkg.com/hexo-theme-material-indigo@latest/css/cloudTie/pc.css',
            '//unpkg.com/hexo-theme-material-indigo@latest/js/cloudTie/pc.min.js',
        ],
        mobileFiles: [
            '//unpkg.com/hexo-theme-material-indigo@latest/css/cloudTie/mobile.css',
            '//unpkg.com/hexo-theme-material-indigo@latest/js/cloudTie/mobile.min.js'
        ]
    };
    </script>
    <script src="//unpkg.com/hexo-theme-material-indigo@latest/js/cloudTie/loader.min.js"></script>







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢您~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.jpg" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <!-- <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循<a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>白小明 &copy; 2017</span>
        </p>
    </div>
</footer> -->

<footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循<a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
            <span>白小明 &copy; 2017</span>
        </p>
    </div>
    <div class="bottom">
        <p>
        <span>
        <a target="_blank" href="mailto:80583600@qq.com" style="border-bottom:none">
        <img src="" alt="E-mail 联系" style="width:20px;height:20px;border:none;">
        </a>
        </span>

        <span>
        <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=80583600&site=qq&menu=yes" style="border-bottom:none">
        <img src="" title="QQ 联系" style="width:22px;height:22px;border:none;"></a>
        </span>

        <span>
        <a target="_blank" href="https://github.com/bxm0927" style="border-bottom:none">
        <img src="" alt="GitHub microzz" title="GitHub 联系" style="width:19px;height:19px;border:none;"></a>
        </span>
        </p>
    </div>
</footer>




    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://www.lovebxm.com/2017/05/30/WPO/&title=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&pic=http://www.lovebxm.com/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://www.lovebxm.com/2017/05/30/WPO/&title=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&source=压缩 HTML、CSS、JS代码压缩技术可以从文件中去掉多余的字符。你在编辑器中写代码的时候，会使用缩进和注释，这些方法会让你的代码简洁而且易读，但它们也..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://www.lovebxm.com/2017/05/30/WPO/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《网站性能优化（Yahoo军规）》 — Xiaoming's Blog&url=http://www.lovebxm.com/2017/05/30/WPO/&via=http://www.lovebxm.com" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://www.lovebxm.com/2017/05/30/WPO/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
